﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SeleniumInputButton.cs" company="">
//   
// </copyright>
// <summary>
//   The selenium input button.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.Selenium.SeleniumControls.HtmlControls
{
    using System.Linq;

    using OpenQA.Selenium;

    using UITest.Selenium.SeleniumContext;

    /// <summary>
    ///     The selenium input button.
    /// </summary>
    public class SeleniumInputButton : SeleniumWebObject
    {
        #region Fields

        private readonly string displayText = string.Empty;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumInputButton"/> class.
        /// </summary>
        /// <param name="seleniumContext">
        /// The selenium context.
        /// </param>
        /// <param name="findBy">
        /// The find by.
        /// </param>
        public SeleniumInputButton(SeleniumContext seleniumContext, By findBy)
            : base(seleniumContext, findBy)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumInputButton"/> class.
        /// </summary>
        /// <param name="seleniumContext">
        /// The selenium context.
        /// </param>
        /// <param name="webElement">
        /// The web element.
        /// </param>
        public SeleniumInputButton(SeleniumContext seleniumContext, IWebElement webElement)
            : base(seleniumContext, webElement)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumInputButton"/> class.
        /// </summary>
        /// <param name="seleniumContext">
        /// The selenium context.
        /// </param>
        /// <param name="displayText">
        /// The display text.
        /// </param>
        public SeleniumInputButton(SeleniumContext seleniumContext, string displayText)
            : base(seleniumContext, By.TagName("input"))
        {
            this.displayText = displayText;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The click.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="timeout">
        /// The timeout.
        /// </param>
        /// <param name="delay">
        /// The delay.
        /// </param>
        public override void Click(string message = "", int timeout = -1, int delay = 500)
        {
            this.WebElement = this.GetWebElement();
            base.Click(message, timeout, delay);
        }

        /// <summary>
        /// The get web element.
        /// </summary>
        /// <returns>
        /// The <see cref="IWebElement"/>.
        /// </returns>
        public override IWebElement GetWebElement()
        {
            return this.FindElements().Select(x => x).First(x => x.GetAttribute("value") == this.displayText);
        }

        #endregion
    }
}